﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    class Player : AnimatedObject2D
    {
        float speed;
        ShotManager shotManager;

        public const float SPEED = 4f;


        public Player(Vector2 posicao, Color cor, Texture2D textura, Animation[] animations)
            : base(posicao, cor, textura, animations)
        {
            //Origem = new Vector2(Retangulo.Width / 2, Retangulo.Height / 2);
        }
        public void LoadShotManager(float d, Texture2D t,
            Animation[] a, float l)
        {
            shotManager = new ShotManager(d,t, a,l);
        }
        public override void Update(GameTime gameTime)
        {
            
            if (Input.GetKey(Keys.A))
            {
                Rotacao-= 0.1f;
            }
            else if (Input.GetKey(Keys.D))
            {
                Rotacao += 0.1f;
            }

            if (Input.GetKey(Keys.W) && speed < SPEED)
            {
                speed += 0.1f;
            }
            else if (Input.GetKey(Keys.S)  && Math.Abs(speed) < SPEED)
            {
                speed -= 0.1f;
            }
            else if (speed > SPEED)
                speed = SPEED;
            else if (speed < -SPEED)
                speed = -SPEED;
            else
            {
                if (speed > 0)
                {
                    speed -= 0.1f;
                }
                else if (speed < 0)
                {
                    speed += 0.1f;
                }
                if (Math.Abs(speed) <= 0.1f)
                {
                    speed = 0;
                }
            }


            if(Input.GetKey(Keys.Right))

            shotManager.Update(gameTime);
                CheckBounds();
            Posicao += new Vector2((float)Math.Sin(Rotacao), -(float)Math.Cos(Rotacao))*speed;
            base.Update(gameTime);

        }

        public void CheckBounds()
        {
            if (!Game1.GWindow.ClientBounds.Intersects(this.Retangulo))
            {
                if (Posicao.X > Game1.GWindow.ClientBounds.Width)
                {
                    Posicao = new Vector2(0, Posicao.Y);
                }
                else if (Posicao.Y > Game1.GWindow.ClientBounds.Height)
                {
                    Posicao = new Vector2(Posicao.X, 0);
                }
                else if (Posicao.X < 0)
                {
                    Posicao = new Vector2(Game1.GWindow.ClientBounds.Width, Posicao.Y);
                }
                else if (Posicao.Y < 0)
                {
                    Posicao = new Vector2(Posicao.X, Game1.GWindow.ClientBounds.Height);
                }
            }
        }
        public int Fire(GameTime gt)
        {
            int i = 0;
            if (Input.GetKey(Keys.Space))
            {
                shotManager.AddShot(gt, Posicao, Math.Abs(speed) * 1.5f + 2,
                  new Vector2((float)Math.Sin(Rotacao), -(float)Math.Cos(Rotacao)),
                  Rotacao);
                i++;
            }
            return i;
        }
        public int CurrentShots()
        {
            return shotManager.ShotCount;
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            
            base.Draw(spriteBatch);
        }
    }
}